package adminMenuDao

import (
	"gitee.com/yangguoliang001/go-gin-vue3/BO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
	"gorm.io/gorm"
)

func (a *adminMenuDao) WhereMenuIdByAdminId(id int32) (menuIdList []model.AdminMenuDO, err error) {
	err = a.db.Debug().Table("admin_menu").Where("admin_id=?", id).Find(&menuIdList).Error
	return
}

func (a *adminMenuDao) CreateAdminMenu(adminMenu []BO.AddAdminMenuBO) (err error) {
	err = a.db.Debug().Table("admin_menu").Create(&adminMenu).Error
	return
}

func (a *adminMenuDao) DeleteAdminMenu(ids []int32) (err error) {
	err = a.db.Debug().Table("admin_menu").Where("id in ?", ids).Delete(&model.AdminMenuDO{}).Error
	return
}

func (a *adminMenuDao) EditAdminMenu(adminMenu []BO.AddAdminMenuBO, ids []int32) (err error) {
	// 开启事务，新增 或 删除
	err = a.db.Transaction(func(tx *gorm.DB) error {
		if len(adminMenu) > 0 {
			return tx.Debug().Table("admin_menu").Create(&adminMenu).Error
		}
		if len(ids) > 0 {
			return tx.Debug().Table("admin_menu").Where("id in ?", ids).Delete(&model.AdminMenuDO{}).Error
		}
		return nil
	})
	return
}
